using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page
{
   
     private void AddLevel2(TreeNode node)
    {
        int ctr = 0;
        while (ctr != 4)
        {
            ctr++;
            TreeNode newNode = new TreeNode();
            switch (ctr)
            {
                case 1:
                    newNode.Text = "Design";
                    break;

                case 2:
                    newNode.Text = "Patent";
                    break;

                case 3:
                    newNode.Text = "Trademark";
                    break;

                case 4:
                    newNode.Text = "Others";
                    break;
            }
            newNode.Value = node.Value + "~" + newNode.Text + "~";
            newNode.SelectAction = TreeNodeSelectAction.Expand;
            newNode.PopulateOnDemand = true;
            node.ChildNodes.Add(newNode);
        }
    }

    protected void btnHeadingSet_Click(object sender, EventArgs e)
    {
    }

    protected void btnRedirect_Click(object sender, EventArgs e)
    {
    }

    protected void EnableDisableProcedureSetTemplate()
    {
        Project prj = new Project();
        string pc = this.txtProjectCode.Text.ToString();
        string pcHeadingSet = "";
        string pcHeadingSetStatus = "";
        bool ResponsiblePersonYN = false;
        string UserId = this.Session["UserId"].ToString();
        prj.GetProjectHeadingSetStatusSp(pc, UserId, out pcHeadingSet, out pcHeadingSetStatus, out ResponsiblePersonYN);
        if (pcHeadingSet.ToString() == "")
        {
            this.btnHeadingSet.Visible = true;
        }
        else if (pcHeadingSetStatus == "New")
        {
            this.btnHeadingSet.Visible = true;
        }
        else
        {
            this.btnHeadingSet.Visible = false;
        }
    }

    private void GetProjectGroups(TreeNode node)
    {
        ProjectGroupList projectgrouplist;
        TreeNode newNode;
        try
        {
            string pgi = null;
            if (this.Session["fromReminder"].ToString() == "1")
            {
                pgi = base.Request.QueryString["projectGroupId"].ToString();
            }
            else
            {
                pgi = base.Request.QueryString["pgi"].ToString();
            }
            projectgrouplist = TreeViewData.GetProjectGroups(pgi, this.Session["Culture"].ToString());
            foreach (ProjectGroup p in projectgrouplist)
            {
                newNode = new TreeNode(p.Title, p.Id);
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                newNode.PopulateOnDemand = true;
                node.ChildNodes.Add(newNode);
            }
        }
        catch (NullReferenceException)
        {
            try
            {
                projectgrouplist = TreeViewData.GetProjectGroups(base.Request.QueryString["pgi"].ToString(), this.Session["Culture"].ToString());
                foreach (ProjectGroup p in projectgrouplist)
                {
                    newNode = new TreeNode(p.Title, p.Id);
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    newNode.PopulateOnDemand = true;
                    node.ChildNodes.Add(newNode);
                }
            }
            catch (NullReferenceException)
            {
            }
        }
    }

    private void GetProjectsForProjectGroup(TreeNode node)
    {
        string projectgroupId = string.Empty;
        string pgi = string.Empty;
        string ptype = string.Empty;
        try
        {
            if (this.Session["fromReminder"].ToString() == "1")
            {
                projectgroupId = base.Request.QueryString["projectGroupId"].ToString() + "~" + base.Request.QueryString["projectType"].ToString() + "~";
            }
            else
            {
                projectgroupId = node.Value;
            }
        }
        catch (NullReferenceException)
        {
            projectgroupId = node.Value;
        }
        try
        {
            pgi = node.Value;
            ptype = "a";
            node.ChildNodes.Clear();
            ProjectList projectlist = TreeViewData.GetProjectsForProjectGroup(pgi, ptype, Convert.ToString(this.Session["OwnerGroupID"].ToString()), Convert.ToBoolean(this.Session["SuperUser"].ToString()), this.Page.User.Identity.Name.ToString());
            foreach (Projects p in projectlist)
            {
                TreeNode newNode = new TreeNode(p.ProjectCode, p.ProjectCode);
                node.ChildNodes.Add(newNode);
            }
        }
        catch (Exception)
        {
        }
    }

    protected void ibExpenditure_Click(object sender, ImageClickEventArgs e)
    {
        this.btnHeadingSet.Visible = false;
        this.lblTitle.Text = Strings.Finance.ToString() + " - " + this.txtProjectCode.Text.ToString();
        this.setIFRAME("Expenses.aspx", 0, 0);
    }

    protected void ibNewProject_Click(object sender, ImageClickEventArgs e)
    {
        this.setIFRAME("ProjectInfo.aspx", 0, 1);
        ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.ProjectManage.ToString();
    }

    protected void ibProjectHeaderInfo_Click(object sender, ImageClickEventArgs e)
    {
        this.setIFRAME("HeaderInfo.aspx", 0, 0);
        this.lblTitle.Text = Strings.ProjectStatusReport.ToString() + " - " + this.txtProjectCode.Text.ToString();
    }

    protected void ibProjectInfo_Click(object sender, ImageClickEventArgs e)
    {
        this.btnHeadingSet.Visible = false;
        string np = "0";
        if (!string.IsNullOrEmpty(base.Request.QueryString["np"]))
        {
            np = base.Request.QueryString["np"].ToString();
        }
        string Reminder = "0";
        try
        {
            if (string.IsNullOrEmpty(this.Session["fromReminder"].ToString()))
            {
                Reminder = "0";
            }
            else
            {
                Reminder = "1";
            }
        }
        catch (Exception)
        {
            Reminder = "0";
        }
        try
        {
            if (!this.Page.IsPostBack | (Reminder.ToString() == "1"))
            {
                this.setIFRAME("ProjectInfo.aspx", 0, Convert.ToInt16(np.ToString()));
            }
            else
            {
                this.setIFRAME("ProjectInfo.aspx", 1, 0);
            }
        }
        catch (NullReferenceException)
        {
            this.setIFRAME("ProjectInfo.aspx", 1, 0);
        }
        this.Session["fromReminder"] = string.Empty;
    }

    protected void ibViewDoc_Click(object sender, ImageClickEventArgs e)
    {
        this.btnHeadingSet.Visible = false;
        this.lblTitle.Text = Strings.ProjectDocument.ToString() + " - " + this.txtProjectCode.Text.ToString();
        this.setIFRAME("ProjectDocument.aspx", 0, 0);
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void lbRespPersons_Click(object sender, EventArgs e)
    {
        this.btnHeadingSet.Visible = false;
        this.setIFRAME("RespPerson.aspx", 0, 0);
        this.lblTitle.Text = Strings.ResponsiblePerson.ToString() + " - " + this.txtProjectCode.Text.ToString();
    }

    protected void OkButton_Click(object sender, EventArgs e)
    {
        string projectCode = this.txtProjectCode.Text.ToString();
        string ownergroupID = this.Session["OwnerGroupID"].ToString();
        foreach (GridViewRow gvRow in this.gvHeadingSet.Rows)
        {
            CheckBox cb = (CheckBox) gvRow.FindControl("cbSelect");
            if ((cb != null) && cb.Checked)
            {
                try
                {
                    new ProjectHeading(projectCode, this.gvHeadingSet.DataKeys[gvRow.RowIndex].Values["HeadingSet"].ToString(), null, "Not Used", this.Page.User.Identity.Name.ToString(), this.Page.User.Identity.Name.ToString(), ownergroupID).InsertProjectHeaderByHeadingSet();
                    this.EnableDisableProcedureSetTemplate();
                }
                catch (ArgumentOutOfRangeException)
                {
                }
            }
        }
        ImageClickEventArgs imgEventArgs = new ImageClickEventArgs(1, 1);
        this.ibProjectHeaderInfo_Click(sender, imgEventArgs);
    }

    private void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {

            string culture = this.Session["Culture"].ToString().ToLower();
            this.ibNewProject.ImageUrl = "~/Images/p_NewProject-" + culture + ".gif";
            this.ibProjectInfo.ImageUrl = "~/Images/p_ProjInfo-" + culture + ".gif";
            this.ibViewDoc.ImageUrl = "~/Images/p_ViewDoc-" + culture + ".gif";
            this.ibProjectHeaderInfo.ImageUrl = "~/Images/p_AppProc-" + culture + ".gif";
            this.ibExpenditure.ImageUrl = "~/Images/p_Finance-" + culture + ".gif";

            ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.ProjectManage.ToString() + "|" + ISitemap.ProjectMaintenance.ToString();
        }
        string Reminder = "0";
        try
        {
            if (string.IsNullOrEmpty(this.Session["fromReminder"].ToString()))
            {
                Reminder = "0";
            }
            else
            {
                Reminder = "1";
            }
        }
        catch (Exception)
        {
            Reminder = "0";
        }
        if (!this.Page.IsPostBack | (Reminder == "1"))
        {
            this.showRow();
            try
            {
                Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProjectMaintenance");
                if (!canView.CanView())
                {
                    base.Response.Redirect("ErrorPage/NotAllowed.aspx");
                }
            }
            catch (Exception)
            {
                base.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script language=javascript>alert('" + Messages.mSessionExpired.ToString() + "');</script>");
            }
            try
            {
                try
                {
                    if (this.Session["fromReminder"].ToString() == "1")
                    {
                        try
                        {
                            this.txtProjectGroupId.Text = base.Request.QueryString["projectGroupId"].ToString();
                        }
                        catch (NullReferenceException)
                        {
                        }
                        try
                        {
                            this.txtProjectCode.Text = base.Request.QueryString["projectCode"].ToString();
                        }
                        catch (NullReferenceException)
                        {
                            this.txtProjectCode.Text = base.Request.QueryString["pc"].ToString();
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    try
                    {
                        this.txtProjectCode.Text = this.Session["projectCode"].ToString();
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
            finally
            {
                ImageClickEventArgs imgEventArgs;
                try
                {
                    imgEventArgs = new ImageClickEventArgs(1, 1);
                    if ((base.Request.QueryString["np"].ToString() == "1") && (base.Request.QueryString["di"].ToString() == "0"))
                    {
                        this.ibProjectInfo_Click(sender, imgEventArgs);
                    }
                }
                catch (NullReferenceException)
                {
                    try
                    {
                        if (this.Session["fromReminder"].ToString() == "1")
                        {
                            imgEventArgs = new ImageClickEventArgs(1, 1);
                            this.ibProjectInfo_Click(sender, imgEventArgs);
                        }
                    }
                    catch (NullReferenceException)
                    {
                    }
                }
            }
        }
        try
        {
            object oSource = new object();
            TreeNodeEventArgs evt = new TreeNodeEventArgs(this.TreeView1.SelectedNode.Parent);
            this.PopulateNode(oSource, evt);
        }
        catch (NullReferenceException)
        {
        }
        this.setImgButtons();
    }

    protected void PopulateNode(object source, TreeNodeEventArgs e)
    {
        try
        {
            if (this.Session["fromReminder"].ToString() == "1")
            {
                if (e.Node.Depth == 0)
                {
                    e.Node.Text = base.Request.QueryString["projectGroupId"].ToString();
                    e.Node.Value = e.Node.Text;
                    this.GetProjectGroups(e.Node);
                }
            }
            else
            {
                switch (e.Node.Depth)
                {
                    case 0:
                        e.Node.Text = base.Request.QueryString["pgi"].ToString();
                        e.Node.Value = e.Node.Text;
                        this.GetProjectGroups(e.Node);
                        goto Label_01C5;

                    case 1:
                        this.GetProjectsForProjectGroup(e.Node);
                        goto Label_01C5;
                }
            }
        }
        catch (NullReferenceException)
        {
            switch (e.Node.Depth)
            {
                case 0:
                    break;

                case 1:
                    this.GetProjectsForProjectGroup(e.Node);
                    goto Label_01C5;

                default:
                    goto Label_01C5;
            }
            try
            {
                e.Node.Text = base.Request.QueryString["pgi"].ToString();
            }
            catch (NullReferenceException)
            {
                try
                {
                    e.Node.Text = base.Request.QueryString["projectGroupId"].ToString();
                }
                catch (NullReferenceException)
                {
                }
            }
            e.Node.Value = e.Node.Text;
            this.GetProjectGroups(e.Node);
        }
    Label_01C5:;
        try
        {
            if (!string.IsNullOrEmpty(this.Session["FromReminder"].ToString()) && (this.Session["FromReminder"].ToString() == "1"))
            {
                switch (e.Node.Depth)
                {
                    case 0:
                        this.txtProjectGroupId.Text = e.Node.ChildNodes[0].Value;
                        return;

                    case 1:
                        this.txtProjectGroupId.Text = e.Node.Parent.Value;
                        return;
                }
            }
        }
        catch (Exception)
        {
        }
    }

    private void setIFRAME(string pagename, int displaylist, int newprj)
    {
        string sPGI = null;
        try
        {
            sPGI = this.txtProjectGroupId.Text.Substring(0, this.txtProjectGroupId.Text.IndexOf("~"));
        }
        catch (ArgumentOutOfRangeException)
        {
            sPGI = this.txtProjectGroupId.Text.ToString();
        }
        if (this.txtProjectCode.Text.Length != 0)
        {
            int isDelete = 0;
            if (base.Request.QueryString["isd"] != null)
            {
                isDelete = Convert.ToInt32(base.Request.QueryString["isd"]);
            }

            Project pj = new Project();
            string ProjectType = "";
            pj.GetProjectType(this.txtProjectCode.Text.ToString(), out ProjectType);
            if (this.btnHeadingSet.Visible)
            {
                base.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<iframe  src='" + pagename + "?pgi=" + sPGI.ToString() + "&type=" + ProjectType + "&pc=" + this.txtProjectCode.Text.ToString() + "&di=" + displaylist.ToString() + "&np=" + newprj.ToString() + "&isd=" + isDelete.ToString() + "'  marginheight='0px' marginwidth='0px'  frameborder='0' height='800px' width='720px' style='position:absolute; top:300px; left:270px;border:0px solid #FFFFFF'></iframe>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<iframe  src='" + pagename + "?pgi=" + sPGI.ToString() + "&type=" + ProjectType + "&pc=" + this.txtProjectCode.Text.ToString() + "&di=" + displaylist.ToString() + "&np=" + newprj.ToString() + "&isd=" + isDelete.ToString() + "'  marginheight='0px' marginwidth='0px'  frameborder='0' height='800px' width='720px' style='position:absolute; top:300px; left:270px;border:0px solid #FFFFFF''></iframe>");
            }
        }
        else
        {
            base.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<iframe  src='" + pagename + "?pgi=" + sPGI.ToString() + "&type=" + this.txtProjectType.Text.ToString() + "&pc=" + this.txtProjectCode.Text.ToString() + "&di=" + displaylist.ToString() + "&np=" + newprj.ToString() + "'  marginheight='0px'  marginwidth='0px' frameborder='0' height='800px' width='720px' style='position:absolute; top:300px; left:270px;border:0px solid #FFFFFF''></iframe>");
        }
    }

    protected void setImgButtons()
    {
    }

    protected void showRow()
    {
        string showRow_javaScript = "<script type='text/javascript' language='javascript'> \n";
        
        //showRow_javaScript = (((showRow_javaScript + (("if (" + this.txtProjectCode.Text.Length) != (0 + ") { \n"))) + "   document.getElementById('tblTitle').style.display = 'inline'; } \n" + "   document.getElementById('tblTitle').style.background-color = 'beige'; } \n") + " else { \n" + "   document.getElementById('tblTitle').style.display = 'inline';  \n") + "   document.getElementById('tblTitle').style.background-color = 'beige'; } \n" + "} </script>";

        showRow_javaScript = (((showRow_javaScript + (("if (" + this.txtProjectCode.Text.Length.ToString()) + "!= 0 ) { \n")) + "   document.getElementById('tblTitle').style.display = 'inline'; } \n" + "    \n") + " else { \n" + "   document.getElementById('tblTitle').style.display = 'inline';  \n") + "    \n" + "} </script>";
        base.ClientScript.RegisterStartupScript(this.Page.GetType(), "regShowRow", showRow_javaScript);
    }

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        try
        {
            TreeNode currentNode = this.TreeView1.SelectedNode;
            TreeNode parentNode = currentNode.Parent;
            currentNode.Select();
            this.txtProjectGroupId.Text = parentNode.Value;
            try
            {
                this.txtProjectCode.Text = currentNode.Text.Substring(0, currentNode.Text.IndexOf("*")).Trim();
            }
            catch (ArgumentOutOfRangeException)
            {
                this.txtProjectCode.Text = currentNode.Text.ToString();
            }
            this.Session["projectCode"] = this.txtProjectCode.Text.ToString();
            ImageClickEventArgs imgEventArgs = new ImageClickEventArgs(1, 1);
            this.showRow();
            this.setImgButtons();
            this.ibProjectInfo_Click(sender, imgEventArgs);
        }
        catch (NullReferenceException)
        {
        }
    }

    protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        this.txtProjectCode.Text = string.Empty;
        ImageClickEventArgs imgEventArgs = new ImageClickEventArgs(1, 1);
        string strname = e.Node.Depth.ToString();
        if ((strname != null) && (strname == "1"))
        {
            this.txtProjectGroupId.Text = e.Node.Parent.Value.ToString();
        }
        else
        {
            this.txtProjectType.Text = "";
        }
        this.showRow();
        this.setImgButtons();
        this.ibProjectInfo_Click(sender, imgEventArgs);

    }

    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        ImageClickEventArgs imgEventArgs = new ImageClickEventArgs(1, 1);
        try
        {
            if (this.Session["fromReminder"].ToString() != "1")
            {
                this.txtProjectCode.Text = string.Empty;
            }
        }
        catch (Exception)
        {
        }
        try
        {
            try
            {
                string strname2 = e.Node.Depth.ToString();
                if (strname2 == null)
                {
                    goto Label_00EA;
                }
                if (!(strname2 == "0"))
                {
                    if (strname2 == "1")
                    {
                        goto Label_00BB;
                    }
                    goto Label_00EA;
                }
                this.txtProjectGroupId.Text = e.Node.ChildNodes[0].Value.ToString();
                this.txtProjectType.Text = "";
                goto Label_013E;
            Label_00BB:
                this.txtProjectGroupId.Text = e.Node.Value.ToString();
                this.txtProjectType.Text = "";
                goto Label_013E;
            Label_00EA:
                this.txtProjectGroupId.Text = "";
                this.txtProjectType.Text = "";
            }
            catch (Exception)
            {
                this.txtProjectGroupId.Text = "";
                this.txtProjectType.Text = "";
            }
        }
        finally
        {
        }
    Label_013E:
        this.showRow();
        this.setImgButtons();
        this.ibProjectInfo_Click(sender, imgEventArgs);

    }
}
